#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin::/usr/local/sbin:/usr/local/bin

pre-install()
{
}

post-install()
{
        set -x
        local dstCR
        dstCR=/usr/local/www/freenasUI
	echo "Adding freenas web gui"
	echo "Making freenas initial database"
        if [ ! -d /data ]; then
		rm -fr /data
		mkdir -p /data
	fi
        cd ${dstCR}

	%%PYTHON_CMD%% manage.py collectstatic --noinput
	%%PYTHON_CMD%% tools/compilemsgs.py

	env FREENAS_INSTALL=yes /usr/local/sbin/migrate93 -f /data/freenas-v1.db
	# Lets be evil here and kill pkg(8) if this fails
	# because pkg does not check return code of POST-INSTALL script
	if [ $? -ne 0 ]; then
		/usr/bin/procstat -h $$|awk '{print $2}'|xargs kill -9
	fi

	env FREENAS_INSTALL=yes %%PYTHON_CMD%% manage.py migrate --fake-initial --noinput --traceback

	# Same as before
	if [ $? -ne 0 ]; then
		/usr/bin/procstat -h $$|awk '{print $2}'|xargs kill -9
	fi

        cd /data
	cp freenas-v1.db factory-v1.db
	chown -R www:www /data
}

backupwarning()
{
}




case $2 in
PRE-INSTALL)
	pre-install
	exit 0
        ;;
POST-INSTALL)
	post-install
	exit 0
	;;
BACKUPWARNING)
	backupwarning
	exit 0
	;;
*)
	exit 1
	;;
esac
